<template>

  <el-dialog
    :title="title"
    :visible.sync="show"
    width="30%"
    top="5vh"
    @close="close"
  >
    <el-form ref="form" :model="role" :rules="rules" class="form" label-position="right" label-width="80px">
      <el-form-item label="角色编号" prop="roleCode">
        <el-input v-model="role.roleCode" />
      </el-form-item>
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="role.roleName" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="role.sort" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-radio v-model="role.state" :label="false">停用</el-radio>
        <el-radio v-model="role.state" :label="true">正常</el-radio>
      </el-form-item>
      <el-form-item label="菜单" prop="menuIds">
        <el-tree
          ref="tree"
          class="form-tree"
          default-expand-all
          :data="tree"
          node-key="id"
          :check-strictly="true"
          :expand-on-click-node="false"
          show-checkbox
          :props="defaultProps"
          @check-change="handleCheckChange"
        />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="role.note" type="textarea" placeholder="请选择" :rows="2" />
      </el-form-item>
      <el-row class="form-button">
        <el-button type="primary" @click="save">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { find, save, update } from '@/api/sys/role'
import { tree } from '@/api/sys/menu'

export default {
  name: 'RoleForm',
  props: {
    id: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      default: '添加字典'
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      role: { roleCode: '', roleName: '', state: true, note: '', sort: 0, menuIds: [] },
      options: [{ label: '停用', value: 0 }, { label: '正常', value: 1 }],
      rules: {
        roleCode: [
          { required: true, message: '请输入岗位编号', trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序值', trigger: 'blur' }
        ]
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      tree: []
    }
  },
  watch: {
    id(val) {
      if (val) { this.find() }
    }
  },
  created() {
    this.findTree()
  },
  methods: {
    close() {
      this.$refs.tree.setCheckedKeys([])
      this.$refs['form'].resetFields()
      this.role = { roleCode: '', roleName: '', state: true, note: '', sort: 0, menuIds: [] }
      this.$emit('update:show', false)
      this.$emit('update:id', null)
    },
    save() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          if (this.id) {
            await update(this.role)
            this.responseRes()
          } else {
            await save(this.role)
            this.responseRes()
          }
        }
      })
    },
    async find() {
      const { data } = await find(this.id)
      this.role = data
      this.$refs.tree.setCheckedKeys(this.role.menuIds)
    },

    responseRes() {
      this.close()
      this.$parent.findPage()
    },
    handleCheckChange() {
      this.role.menuIds = this.$refs.tree.getCheckedKeys()
    },
    async findTree() {
      const { data } = await tree()
      this.tree = data
    }
  }
}
</script>

<style scoped>
.form-tree{
  height: 30vh;
  overflow-y:auto;
}
</style>
